'''
Created on 2010-10-19

@author: DouglasWin7
'''
import os

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app

from accessor.comment import comment
from accessor.distinction import distinction
from accessor.distinctiondetail import distinctiondetail
from rate.equal_repayment_of_principal import equal_repayment_of_principal


class MainPage(webapp.RequestHandler):
    def get(self):
        template_values={"comments":comment.get_comments()}
        load_page(self,template_values,'index.xhtml')
           
    def post(self):
        user = users.get_current_user()
        comment.add_comment(user.nickname(),
                            self.request.get("name"),
                            self.request.get("subscribe"))
        template_values={"comments":comment.get_comments()}
        load_page(self,template_values,'index.xhtml')

class MainPointPage(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        template_values={"distinctions": distinction.get_all_distinctions(),
                         "name":user.nickname()}
        load_page(self,template_values,'point.xhtml')
    
class AddPointPage(webapp.RequestHandler):
    def get(self):
        load_page(self,{},'add_distinction.xhtml')
    def post(self):
        distinctiondetail.add_distinctiondetail(self.request.get("name"), 
                                                int(self.request.get("point")), 
                                                self.request.get("description"))
        self.redirect('/point')
    
class ViewPointPage(webapp.RequestHandler):
    def get(self):
        name = self.request.str_GET['name']
        load_page(self,
                  {"details":distinctiondetail.get_distinctiondetail(name)},
                  'view_distinctiondetail.xhtml')
    
class DeletePointPage(webapp.RequestHandler):
    def get(self):
        name = self.request.str_GET['name']
        distinctiondetail.delete_distinction(name)
        self.redirect('/point')

class ViewRatePage(webapp.RequestHandler):
    def get(self):
        load_page(self,
                  {"list":None,
                  "amount":340000,
                  "monthcount":240,
                  "rate":0.04158},
                  'view_my_rate.xhtml')
    def post(self):
        amount = int(self.request.get('amount'))
        monthcount = int(self.request.get('monthcount'))
        rate = float(self.request.get('rate'))        
        load_page(self,
                  {"list":equal_repayment_of_principal.get_list(amount, monthcount, rate),
                  "amount":amount,
                  "monthcount":monthcount,
                  "rate":rate},
                  'view_my_rate.xhtml')

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/point', MainPointPage),
                                      ('/view_point',ViewPointPage),
                                      ('/add_point',AddPointPage),
                                      ('/delete_point',DeletePointPage),
                                      ('/view_my_rate',ViewRatePage)],
                                     debug=True)

def load_page(page, template_values, filename):
    user = users.get_current_user()
    if user:
        path = os.path.join(os.path.dirname(__file__), filename)
        page.response.out.write(template.render(path,template_values))
    else:
        page.redirect(users.create_login_url(page.request.uri))

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()